define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/device/index' + location.search,
                    add_url: 'device/device/add',
                    edit_url: 'device/device/edit',
                    del_url: 'device/device/del',
                    multi_url: 'device/device/multi',
                    import_url: 'device/device/import',
                    table: 'device',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: "ID",},
                        {field: 'device_id', title: __('设备编号'), operate: 'LIKE'},
                        {field: 'device_sn', title: __('主板编号'), operate: 'LIKE'},
                        {field: 'device_type', title: __('Device_type'), operate: 'LIKE'},
                        {field: 'device_name', title: __('Device_name'), operate: 'LIKE'},
                        {field: 'online_status', title: __('在线状态'), searchList: {"1": __('在线'), "0": __('离线')}},
                        {field: 'user.mobile', title: __('代理商')},
                        {field: 'qr_code', title: __('二维码'),events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'create_time', title: __('时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: [

                                {
                                    name: 'detail',
                                    title: __('货道管理'),
                                    text: "货道管理",
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-user-circle',
                                    url: function (row) {
                                        return "device/deviceSlot/index?device_id=" + row['device_id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('设备订单'),
                                    text: "设备订单",
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-bitcoin',
                                    url: function (row) {
                                        return "order/deviceorder/index?device_id=" + row['device_id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('出货记录'),
                                    text: "出货记录",
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-bitcoin',
                                    url: function (row) {
                                        return "device/devicecmd/index?device_id=" + row['device_id'];
                                    }
                                }
                                ]
                        }]
                ]
            });


            $(document).on("click", ".btn-export", function () { //监听刚刚的按钮btn-myexcel-export的动作

                top.location.href="/admin/device/device/export";
            });

            $(document).on("click", ".btn-download", function () { //监听刚刚的按钮btn-myexcel-export的动作

                var ids = Table.api.selectedids(table);
                console.info(ids);
                Table.api.multi("changestatus",ids.join(","),table,this);
                top.location.href="/admin/device/device/download?ids="+ids.join(',');

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },




        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
